package com.bjtime.hprinter.Fragments.attrs


import android.graphics.Color
import android.os.Bundle
import android.support.v4.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.CheckBox
import android.widget.ImageButton
import android.widget.LinearLayout
import android.widget.TextView
import com.bjtime.designer.attributes.QRCodeAttr
import com.bjtime.designer.utils
import com.bjtime.designer.widget.BarCodeView2
import com.bjtime.designer.widget.BaseLabelView
import com.bjtime.designer.widget.QRCodeView2
import com.bjtime.hprinter.Dialogs.MIUIInputDialog
import com.bjtime.hprinter.R
import kotlinx.android.synthetic.main.viewpager_attrs_layout_base.*


class barcode : Fragment , View.OnClickListener {

    constructor()

    companion object {
        fun newInstance () : barcode {
            return barcode ()
        }
    }

    private var BRView : BarCodeView2? = null
    private var QRText : TextView    ? = null


    fun setView (v : BaseLabelView) {
        BRView = v as BarCodeView2
        if (BRView != null) {
            QRText!!.setText(BRView!!.des_text)
            mTextSize!!.setText(BRView!!.getTextSize().toString())
        }
    }


    private var mView : View? = null
    private var BRCODE : TextView? = null
    private var mTextAdd : ImageButton? = null
    private var mTextDec : ImageButton? = null
    private var mTextSize : TextView? = null

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        mView = LayoutInflater.from(context).inflate(R.layout.viewpager_attrs_layout_barcode, null)
        mView!!.findViewById<CheckBox>(R.id.attrs_base_expand).setOnCheckedChangeListener({ i, b->
            if (b) {
                mView!!.findViewById<View>(R.id.attrs_base_layout).visibility = View.VISIBLE
                text1.setText(resources.getString(R.string.bastattr_left))
                (text1.parent as LinearLayout).setBackgroundColor(Color.TRANSPARENT)
            } else {
                mView!!.findViewById<View>(R.id.attrs_base_layout).visibility = View.GONE
                text1.setText(resources.getString(R.string.bastattr_expand))
                (text1.parent as LinearLayout).setBackgroundColor(Color.WHITE)
            }
        })

        QRText = mView!!.findViewById(R.id.br_tv_settext)


        mTextAdd = mView!!.findViewById(R.id.bar_attr_text_add)
        mTextDec = mView!!.findViewById(R.id.bar_attr_text_dec)
        mTextSize = mView!!.findViewById(R.id.bar_attr_text_size)

        mTextAdd!!.setOnClickListener(this)
        mTextDec!!.setOnClickListener(this)

        BRCODE = mView!!.findViewById(R.id.br_tv_settext)
        BRCODE!!.setOnClickListener(this)

        return mView
    }




    override fun onClick(v: View?) {
        when (v) {
            BRCODE -> {
                var dialog  = MIUIInputDialog(context!!)
                dialog.setTitle("条形码内容")
                dialog.show()
                dialog.setMaxLenght(80)
                dialog.setString (QRText!!.text.toString())


                dialog.setOnButtonClickListener(object : MIUIInputDialog.OnButtonClick {
                    override fun onOk(v: View) {
                        QRText!!.setText(dialog.getString ())
                        if (BRView != null)
                            BRView!!.setText(QRText!!.text.toString())
                    }

                    override fun onCancel(v: View) {

                    }
                })
            }
            mTextAdd -> {
                if (BRView != null)
                    BRView!!.setTextSize(utils.px2sp(context!!, BRView!!.getTextSize().toFloat()) + 2)
            }
            mTextDec -> {
                if (BRView != null)
                    BRView!!.setTextSize(utils.px2sp(context!!, BRView!!.getTextSize().toFloat()) - 2)
            }
        }

    }
}